package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-09-06
 * Time: 10:38
 */
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/hello")
    public String sayHello() {
        return "hello SpringMVC!";
    }
    @RequestMapping("/hello2")
    public String sayHello2() {
        return "";
    }
    @RequestMapping("/getname")
    public void getName(@RequestParam(value = "n",required = false) String name, String pwd) {
        System.out.println("name："+name+"；pwd："+pwd);
    }
    @RequestMapping("/getuser")
    public void getUser(User user) {
        System.out.println(user.toString());
    }
    @RequestMapping("/getjson")
    public void getJson(@RequestBody User user) {
        System.out.println(user.toString());
    }
    @RequestMapping("/geturlpart/{name}/{pwd}")
    public void getUrlpart(@PathVariable String name,@PathVariable("pwd") String password) {
        System.out.println("name: "+name+" ;password: "+password);
    }
    @RequestMapping("/upload")
    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
        // 1.生成一个唯一 id
        String id = UUID.randomUUID().toString().replace("-","");
        // 2.获取源文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 3.设置文件完整保存路径
        String path = "D:/resource/"+id+suffix;
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/getheader")
    public void header(@RequestHeader("User-Agent") String userAgent) {
        System.out.println("userAgent："+userAgent);
    }
    @RequestMapping("/getcookie")
    public void getCookie(@CookieValue(value = "mycook",required = false) String mycookie) {
        System.out.println("mycookie: "+mycookie);
    }
    @RequestMapping("/getsession")
    public void sess2(@SessionAttribute(value = "username",required = false) String username) {
        System.out.println("username："+username);
    }

    @RequestMapping("get1")
    public String get1(){
        return "<h1>标题1</h1>";
    }
}
